﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ObjectLayer;
using System.Data;
using System.IO;

namespace BSC.report
{
    public partial class r_cost_revenues : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
            Global.GlobalVariable.UploadPath=Server.MapPath(@"~/content/download/");
            //AutoPostBackControl = false;
            //List<ObjectLayer.fact_costs_revenuesBO> lst = new List<ObjectLayer.fact_costs_revenuesBO>();
            //lst = AccessLayer.fact_costs_revenuesDAO.Get();
            //DataTable dt = Lib.Collections.ConvertToDataTable(lst);
            //rpt_cost_revenues report = new rpt_cost_revenues();
            //CrystalReportViewer1.ReportSource = report;
            //report.SetDataSource(lst);
            //ReportViewer1.LocalReport.Refresh();
            
        }
        static List<ObjectLayer.fact_costs_revenuesBO> lst;
        [System.Web.Script.Services.ScriptMethod(ResponseFormat = System.Web.Script.Services.ResponseFormat.Json)]
        [System.Web.Services.WebMethod]
        public static string GetCost(string from,string to)
        {
            int? pFrom=null, pTo=null;
            if (from == "") pFrom = null; else pFrom = int.Parse(from);
            if (to == "") pTo = null; else pTo = int.Parse(to);
            lst = AccessLayer.fact_costs_revenuesDAO.Get(pFrom
                ,pTo);
            //Context.Response.Write(new JavaScriptSerializer().Serialize(lst));
            var tmp = Newtonsoft.Json.JsonConvert.SerializeObject(lst, Newtonsoft.Json.Formatting.Indented); //"adsdasdasds";

            return tmp;// +"dsdasd";// new JavaScriptSerializer().Serialize(lst.ToArray());//Newtonsoft.Json.JsonConvert.SerializeObject(lst);
        }
        [System.Web.Script.Services.ScriptMethod(ResponseFormat = System.Web.Script.Services.ResponseFormat.Json)]
        [System.Web.Services.WebMethod]
        public static string ExportData()
        {
            DataTable dt = Lib.Collections.ConvertToDataTable(lst);
#region Set Caption
            foreach (DataColumn c in dt.Columns)
            {
                switch (c.ColumnName)
                {
                    case ("timekey"):
                        {
                            c.Caption = "Time key";
                            break;
                        }
                    case ("month"):
                        {
                            c.Caption = "Tháng";
                            break;
                        }
                    case ("branch_code"):
                        {
                            c.Caption = "Mã khu vực";
                            break;
                        }
                    case ("branch_name"):
                        {
                            c.Caption = "Khu vực";
                            break;
                        }
                    case ("cost_code"):
                        {
                            c.Caption = "STT";
                            break;
                        }
                    case ("criteria_name"):
                        {
                            c.Caption = "Tiêu chí";
                            break;
                        }
                    case ("unit_code"):
                        {
                            c.Caption = "Mã ĐVT";
                            break;
                        }
                    case ("unit_name"):
                        {
                            c.Caption = "ĐVT";
                            break;
                        }
                    case ("accumulate"):
                        {
                            c.Caption = "Lũy kế";
                            break;
                        }
                    case ("type"):
                        {
                            c.Caption = "Loại GD";
                            break;
                        }
                    case ("cost"):
                        {
                            c.Caption = "Giá trị";
                            break;
                        }
                }
            }
#endregion
            string[] valueFields = new string[] { "cost|Sum", "accumulate|Sum" };
            string[] rowsFields = new string[] { "month","type", "cost_code", "criteria_name", "unit_name" };
            string[] colFields = new string[] { "branch_code", "branch_name" };

            string file = DateTime.Now.ToString("yyyyMMddhhmmss")+".xls";

            Lib.ExcelProcess.CreatePivot(Global.GlobalVariable.UploadPath + file, dt, "Summary", valueFields, colFields, rowsFields);
            if (File.Exists(Global.GlobalVariable.UploadPath +file))
            {
                return "/content/download/"+file;
            }
            return "";

        }
    }
}
        
